/**
 * Copyright (c) 2009 Blue Whale Systems Ltd
 * 
 * This software is provided "as is," and the copyright holder makes no representations or warranties, express or
 * implied, including but not limited to warranties of merchantability or fitness for any particular purpose or that the
 * use of this software or documentation will not infringe any third party patents, copyrights, trademarks or other
 * rights.
 * 
 * The copyright holder will not be liable for any direct, indirect special or consequential damages arising out of any
 * use of this software or documentation.
 */
package com.bluewhalesystems.client.lwuit.richtext;

import com.sun.lwuit.*;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.geom.*;
import com.sun.lwuit.util.EventDispatcher;

public class DefaultRichTextAreaModel implements RichTextAreaModel
{
    public void setDataForWidth( int aWidth, GrowableCharArray aData, int aHeight )
    {
        // Not supporting multiple widths at the moment.
        iData = aData;
        iHeight = aHeight;
    }

    // RichTextAreaModel

    public char[] getData( int aComponentWidth, int aScrollPosition, int[] aParameters )
    {
        char[] data = null;

        if( null != iData )
        {
            data = iData.getArray();
            aParameters[RichTextAreaModel.PARAMETER_VALID_LENGTH] = iData.length();

            // Bypass the very first character (which should be the DATA_VERSION or 'A').
            aParameters[RichTextAreaModel.PARAMETER_OFFSET_FOR_POSITION] = 1;
            aParameters[RichTextAreaModel.PARAMETER_HEIGHT_AT_OFFSET_FOR_POSITION] = 0;
        }

        return data;
    }

    public int getComponentHeight( int aComponentWidth )
    {
        return iHeight;
    }

    public Painter getObjectPainter( int aId )
    {
        return PAINTER;
    }

    private static class ObjectPainter implements Painter
    {
        // Painter

        public void paint( Graphics aGraphics, Rectangle aRectangle )
        {
            int color = aGraphics.getColor();
            Dimension size = aRectangle.getSize();

            switch( iId )
            {
                case 0:
                    aGraphics.setColor( 0xAAAAAA );
                    aGraphics.fillRect( aRectangle.getX(), aRectangle.getY() + ( size.getHeight() / 2 ), size.getWidth(), 1 );
                    break;
            }

            aGraphics.setColor( color );
        }

        int iId;
    }

    private static final ObjectPainter PAINTER = new ObjectPainter();

    private GrowableCharArray          iData;
    private int                        iHeight;
}